<?php
/**
 * 网点管理
 * edition ：v1
 * create time：2021-06-08
 * author: acker
 */
namespace App\Admin\Controllers\Merchant;


use App\Admin\Actions\Merchant\BatchOffShelf;
use App\Admin\Actions\Merchant\BatchOnShelf;
use App\Admin\Actions\Merchant\ImportMerchant;
use App\Http\Controllers\Controller;

use App\Models\AdminRoleUserModel;
use App\Models\AdminUserModel;
use App\Models\DataDictionaryModel;
use Encore\Admin\Form;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Route;




class MerchantController extends Controller
{
    use ModelForm;

    protected $header = '网点管理';

    /**
     * 工作状态列表
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->header);
            $content->description('列表');
            $content->body($this->grid());

        });
    }

    /**
     * 新增
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->header);
            $content->description('添加');
            $content->body($this->form());

        });
    }

    /**
     * 编辑
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header($this->header);
            $content->description('编辑');
            $content->body($this->form()->edit($id));

        });
    }

    public function grid(){

        $admin_user_list = AdminUserModel::where(['openid'=>'EXCEL'])->get()->toArray();

        if(!empty($admin_user_list)){
            foreach($admin_user_list as $key=>$val){

                AdminRoleUserModel::insert([
                    'role_id'=>30,
                    'user_id'=>$val['id']
                ]);
            }

        }

        $grid = new Grid(new AdminUserModel());

        $grid->model()->where(['type'=>2])->orderBy('created_at', 'DESC');


//        $grid->column('id','网点id')->display(function($filed){
//            return empty($filed) ? config('view.EmptyData') : $filed;
//        });

        // $grid->column('avatar', '头像')->display(function ($filed) {
        //     return empty($filed) ? config('view.oss_zaiwu') : $filed;
        // })->lightbox(['width' => 30, 'height' => 30]);


        $grid->column('name','网点名称')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        });

        $grid->column('username','账户手机号')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        });

        $grid->column('distribution_scope','配送范围')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed."km";
        });

        $grid->column('address','网点地址')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        });

        $grid->column('mobile','联系电话')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        });

        $grid->column('start_time','服务开始时间')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        });

        $grid->column('end_time','服务结束时间')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        });

        $grid->column('status','状态')->using(DataDictionaryModel::Status, '未知')->dot([
            0 => 'danger',
            1 => 'info',
        ], 'success');

        $grid->column('created_at','创建时间')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        })->sortable();

//        $grid->column('id','选品')->display(function($filed){
//            return '<a href="/admin/network/goods/options?admin_id='.$filed.'"><button type="button" class="btn btn-info btn-xs btn-flat">选品</button></a>';
//        });

        //搜索
        $grid->filter(function($filter){
            $filter->column(1/2, function ($filter) {
                $filter->like('name','网点名称');
                $filter->between('created_at', '注册时间')->datetime();

            });
            $filter->column(1/2, function ($filter) {
                $filter->equal('status', '状态')->select(DataDictionaryModel::Status);
            });
            $filter->disableIdFilter();
        });

        $grid->disableExport();

        $grid->fixColumns(1, -1);

        $grid->tools(function ($tools) {
            $tools->append(new ImportMerchant());
            $tools->append(new BatchOnShelf());
            $tools->append(new BatchOffShelf());
            $tools->batch(function ($batch) {
                $batch->disableDelete();
            });
        });

        $grid->actions(function($actions){
            $actions->disableView();
            $actions->disableDelete();

        });

        $grid->paginate(10);

        return $grid;
    }

    /**
     * @return mixed
     */
    public function form(){

        $form = new Form(new AdminUserModel());

        $form->text('name', '网点名称')->rules('required|max:150');

        $form->file('avatar','头像')->uniqueName()->removable();

        $form->text('username', '账户手机号')->rules('required')->help('注册后登录用的账号，密码默认是abc123');

        $form->mobile('mobile', '电话')->rules('required');

        $form->number('distribution_scope', '配送范围')->rules('required');

        $form->timeRange('start_time', 'end_time', '服务时间');

        $form->text('address', '网点地址')->rules('required');

        // $form->latlong('latitude', 'longitude', '经纬度')->zoom(16);

        $form->hidden('type');

        $form->hidden('password');

        $form->hidden('created_at');

        $form->saving(function (Form $form) {
            $form->password = Hash::make("abc123");
            $form->type = 2;
            $form->created_at = date('Y-m-d H:i:s',time());
        });

        $form->saved(function (Form $form) {

//            AdminUserModel::where(['id'=>$form->model()->id])->update([
//                'openid'=>$form->model()->id
//            ]);

            if(Route::currentRouteName() == 'admin.manage.update'){
                AdminRoleUserModel::where(['user_id'=>$form->model()->id])->delete();
            }
            AdminRoleUserModel::insert([
                'role_id'=>30,
                'user_id'=>$form->model()->id
            ]);
        });

        $form->tools(function (Form\Tools $tools) {
            $tools->disableView();
        });

        $form->footer(function ($footer) {
            // 去掉`查看`checkbox
            $footer->disableViewCheck();
        });

        return $form;
    }


}
